package com.fruit.service.redis.impl;

import java.util.Map;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fruit.constants.RedisConstant;
import com.fruit.pojo.user.UserInfo;
import com.fruit.service.redis.JedisUtil;
import com.fruit.service.redis.UserRedisUtil;
import com.fruit.util.CustomUtil;
import com.fruit.util.ShortUrlUtil;


/**
 * 
 * @author admin
 * @version 2017.06.09 10:00
 * 
 */
@Service
public class UserRedisService implements UserRedisUtil {

   
    @Autowired
    private JedisUtil jedisService;

	@Override
	public String makeUserToken(int userId, String key) {
		String nonce = ShortUrlUtil.buildCode(8);
		String timestamp = Long.toString(System.currentTimeMillis()); 
		Map<String, String> params = new TreeMap<String, String>();
		params.put("userId", Integer.toString(userId));
		params.put("nonce", nonce);
		params.put("timestamp", timestamp);
		String userToken = CustomUtil.createSign(params, key);
		return userToken;
	}

	@Override
	public String getUserToken(String key) {
		return jedisService.get(key);
	}

	@Override
	public void setUserToken(String key, String value) {
		jedisService.set(key, value);
		jedisService.expire(key, 1800);
	}


	@Override
	public void setUserToken(String key, String value, int expire) {
		jedisService.set(key, value);
		jedisService.expire(key, expire);
		
	}

	@Override
	public void setUserTokenExpire(String key) {
		jedisService.expire(key, 1800);
	}

	@Override
	public void setUserToken(UserInfo result, String userToken) {
		String value = result.getId()+"_"+result.getLoginId();
		setUserToken(RedisConstant.WAP_USER_TOOKEN+userToken,value);
	}

	@Override
	public void removeUserToken(String key) {
		jedisService.del(key);
	}

	@Override
	public boolean isExists(String key) throws Exception{
		return jedisService.exists(key);
	} 
    
    
    
}
